﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Action : System.Web.UI.Page
{
    private int _cid = -1;
    protected int CID
    {
        get { return _cid; }
        set { _cid = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CID = String.IsNullOrEmpty(Request.QueryString["cid"])
                ? -1 : Int32.Parse(Request.QueryString["cid"].ToString());
            CIDlabel.Text = Request.QueryString["cid"].ToString();
            ContractBind();
            if (!String.IsNullOrEmpty(Request.QueryString["act"]))
            {
                if (Request.QueryString["act"].Equals("pay", StringComparison.CurrentCultureIgnoreCase))
                {
                    Payed.Visible = true;
                    Renew.Visible = false;
                }
                else
                {
                    Payed.Visible = false;
                    Renew.Visible = true;
                }
            }
        }
    }

    protected void Payed_Click(object sender, EventArgs e)
    {
        string sqlstr = "UPDATE CW_CONTRACT_T SET PayDate = date(paydate,'+1 year') WHERE CID = $CID";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$CID", DbType.Int32)
        };
        spc[0].Value = Int32.Parse(CIDlabel.Text);
        int res = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        if (res == 1)
        {
            Response.Redirect("Warning.aspx?action=pay");
        }
        else
        {
            msg.Text = "付款未成功！请重试";
        }
    }

    private void ContractBind()
    {
        string sqlstr = @"SELECT T2.DataName AS Region,StationName,StationType,OtherName,ContractNo,BeginDate,EndDate,PayDate,ContractValue,PayMode,ContactInfo,AccountName,OpenBank,AccountNo
            ,Desc FROM cw_Contract_t T1 LEFT JOIN CW_PUBDATA_T T2 ON T1.Region = T2.DataCode AND T2.DataType = 'Region' WHERE CID = $CID";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$CID", DbType.Int32)
        };
        spc[0].Value = CID;
        DataRow cont = SQLiteHelper.ExecuteDataRow(sqlstr, spc);
        CIDlabel.Text = CID.ToString();
        Region.Text = cont["Region"].ToString();
        StationName.Text = cont["StationName"].ToString();
        StationType.Text = cont["StationType"].ToString();
        OtherName.Text = cont["OtherName"].ToString();
        ContractNo.Text = cont["ContractNo"].ToString();
        BeginDate.Text = DateTime.Parse(cont["BeginDate"].ToString()).ToString("yyyy-MM-dd");
        EndDate.Text = DateTime.Parse(cont["EndDate"].ToString()).ToString("yyyy-MM-dd");
        PayDate.Text = DateTime.Parse(cont["PayDate"].ToString()).ToString("yyyy-MM-dd");
        ContractValue.Text = cont["ContractValue"].ToString();
        PayMode.Text = cont["PayMode"].ToString();
        ContactInfo.Text = cont["ContactInfo"].ToString();
        AccountName.Text = cont["AccountName"].ToString();
        OpenBank.Text = cont["OpenBank"].ToString();
        AccountNo.Text = cont["AccountNo"].ToString();
        Desc.Value = cont["Desc"].ToString();
    }
    protected void Renew_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewContract.aspx?renew=yes&cid=" + CIDlabel.Text);
    }
}
